from PyQt5.QtCore import QSize, Qt, pyqtProperty, QRect, QPoint, pyqtSignal
from PyQt5.QtGui import QIcon, QPixmap, QPainter, QDragEnterEvent, QDropEvent
from PyQt5.QtWidgets import QWidget


class DragEventBase:
    dropUrl = pyqtSignal(str)

    def dragEnterEvent(self, event: QDragEnterEvent):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event: QDropEvent):
        for url in event.mimeData().urls():
            if url.isLocalFile():
                self.dropUrl.emit(url.toLocalFile())


class IconWidget(QWidget):
    """ Icon widget

    Constructors
    ------------
    * IconWidget(`parent`: QWidget = None)
    * IconWidget(`icon`: QIcon | str | FluentIconBase, `parent`: QWidget = None)
    """

    def __init__(self, file: str = None, parent=None):
        super().__init__(parent)
        self.setIcon(QIcon(file))

    def getIcon(self):
        return self._icon

    def setIcon(self, icon: QIcon):
        self._icon = icon
        self.update()

    def paintEvent(self, e):
        painter = QPainter(self)
        painter.setRenderHints(
            QPainter.RenderHint.Antialiasing | QPainter.RenderHint.SmoothPixmapTransform
        )
        icon = QIcon(self._icon)
        icon.paint(painter, QRect(QPoint(2, 2), self.rect().size()), Qt.AlignmentFlag.AlignCenter,
                   state=QIcon.State.Off)

    icon = pyqtProperty(QIcon, getIcon, setIcon)
